'use strict'

const fs=require('fs');
const request=require('request');
const path=require('path');
const config=require('./config');
const analyze=require('./analyze');
var arr = [];

    function start(){
        for(var i = 0 ; i < 200 ; i += 25){
            request(config.url,function(err,res,body){
                console.log('start');
                if(!err && res){
                    console.log('start');
                    analyze.findImg(body,downLoad,arr);
                }
            })
        }
    }
    
    function downLoad(imgUrl,i){
        let ext=imgUrl.split('.').pop();
        request(imgUrl).pipe(fs.createWriteStream(path.join(config.imgDir,i+'.'+ext),{
            'enconding':'binary'
        }))
        console.log(i);
    }
    start();


var initDb=require('./utls/initDb');

// initDb();

var article = require('./models/articles');

var Crawler = require("crawler");

for(var i = 0 ; i < 200 ; i += 25){

    var URL = 'https://book.douban.com/top250?start='+i;

    var c = new Crawler({
        maxConnections : 10,
        // 将为每个抓取的页面调用此命令
        callback : function (error, res, done) {
            if(error){
                console.log(error);
            }else{
                //默认的查找库
                var $ = res.$;
                //$默认为cheerio
                //专为服务器控制台设置的核心Jquery
                var list = $('#content .article table').toArray();
                list.forEach(element => {
                    //.trim()删除空格,.replace正则：
                    var txt=$(element).find(' .pl2 a').text().trim().replace(/[\n\r]/g,'');
                    var bauthor = $(element).find('.pl').text().trim().replace(/([/][^/]+){3,4}$/, '');
                    var brief = $(element).find('.inq').text().trim().replace(/[\n\r]/g,'');
                    article.create({title:txt,bauthor:bauthor,brief:brief});
                });
                //console.log(list);
                // console.log($("title").text());
            }
            done();
        }
    });
    c.queue(URL);
}

